<?php
	//Debugger::VarDump($this->data[$this->name]);
	
	if (HTTPContext::$session['DepositSuccess'] == '1')
	{
		//message
		HtmlsHelper::Message(array("text" => "You have successfully made a deposit!"));
		unset($_SESSION['DepositSuccess']);
	}
	
	if (HTTPContext::$session['DepositSuccess'] == '0')
	{
		//message
		HtmlsHelper::Message(array("text" => "Deposit was no made :("));
		unset($_SESSION['DepositSuccess']);
	}
	
	if (HTTPContext::$session['WithdrawSuccess'] == '1')
	{
		//message
		HtmlsHelper::Message(array("text" => "You have successfully withdrawed deposit!"));
		unset($_SESSION['WithdrawSuccess']);
	}
	
	if (HTTPContext::$session['WithdrawSuccess'] == '0')
	{
		//message
		HtmlsHelper::Message(array("text" => "Deposit was not withdrawed!"));
		unset($_SESSION['WithdrawSuccess']);
	}
	
	echo "
		<div class='pageHeading'>" . Language::Get('Your Deposits') . "</div>
		<table cellspacing='0' cellpadding='4' border='0' width='100%' class='messageList'>
		<tr>
			<th>". Language::Get('deposit amount') . "</th>
			<th>". Language::Get('deposit period') . "</th>
			<th>". Language::Get('percentage') . "</th>
			<th>". Language::Get('deposit date') . "</th>
			<th>". Language::Get('withdraw date') . "</th>
			<th>". Language::Get('current money') . "</th>
			<th>". Language::Get('actions') . "</th>
		</tr>";
	
	if (count ($this->data[$this->name]) == 0)
	{
		echo "<tr><td colspan=7>No records</td></tr>"; 
	}
	
	foreach ($this->data[$this->name] as $key => $value)
	{		
		echo "<tr>
				<td>" . $value['original_sum'] . "$</td>
				<td>" . $value['type_name'] . "</td>
				<td>" . $value['type_percent'] . "%</td>
				<td>" . $value['date'] . "</td>
				<td>" . $value['withdraw_date'] . "</td>
				<td><span id='current_sum_" . $value['id'] . "'>" . $value['current_sum'] . "</span>$</td>
				<td>
					<a href = '#' onclick='Withdraw(" . $value['id'] . ");'> ". Language::Get('withdraw') . "</a>
			</td>
			</tr>";		
	}
		
	echo "</table>";
	
	//pagination
	if (count ($this->data[$this->name]) != 0)
	{
		echo "<div style = 'padding: 15px; '>".HtmlsHelper::Pagination($this->data['num_rows'])."</div>";
	}
	
	//New deposit
	echo "
		<div class='pageHeading'>" . Language::Get('New deposit') . "</div>";
	XHTML::Form('/deposits/create/', FormMethod::Post, array('id' => 'depositForm'));
	XHTML::InputHidden('client_id', HTTPContext::$session['client']['id']);
	echo "
        <table cellspacing='0' cellpadding='0' border='0' style = 'margin-top: 15px;'>";
	// type (period)
	echo "
			<tr>
				<td width = 150px>" . Language::Get('period', 'CAPITALIZE') . ":</td>
				<td class='inputType-1-left'></td>
				<td class='inputType-1-center'>";
					XHTML::Select('type_id', $this->data['DepositTypes'], '', false, array('style' => 'border: 0px; width: 235px;'));
	echo "
				</td>
				<td class='inputType-1-right'></td>
			</tr>
			<tr><td colspan=4 style='height:7px;'></td></tr>";
	// amount
	echo "	<tr>
				<td width = 150px>" . Language::Get('Amount') . "</td>
				<td class='inputType-1-left'></td>
				<td class='inputType-1-center'>";
					XHTML::InputText('original_sum', 1, 'id="depositSum" onchange="SumChange();"');
	echo "
				</td>
				<td class='inputType-1-right'></td>
			</tr>
				<td width=100>&nbsp;</td>
					
			</tr>";
	//submit
	echo "	<tr>
				
		</table>
		
		<table cellspacing='0' cellpadding='0' border='0' style = 'margin-top: 15px;'>
			<tr>
				<td class='btnType-5-left btnType-5-left-select'></td>
				<td class='btnType-5-center btnType-5-center-select' align='center' width=130px>
					<span class='btnType-5-txt cpointer' onclick='BeforeSubmit()'>".Language::Get('apply deposit')."</span>
				</td>
				<td class='btnType-5-right btnType-5-right-select'></td>
			</tr>
		</table>
	</form>"; 
?>

<script type="text/javascript">

	var money = <?php echo $this->data['Client']['sum']; ?>;

	var depositStates = [];
	var depositSums = [];
	var depositCoef = [];
	
	<?php 
		foreach ($this->data[$this->name] as $key => $value)
		{
			echo "depositStates[" . $value['id'] . "] = " . $value['is_ended'] . ";";
			echo "depositSums[" . $value['id'] . "] = " . $value['current_sum'] . ";";

			//second increment
			echo "
			var sum = parseInt(" . $value['original_sum'] . ");
			var percentc = parseInt(" . $value['type_percent'] . ");
			
			for(var i = 1; i <= parseInt(" . $value['type_period'] . "); i++)
			{
				sum += (sum * percentc) / 100;
			}";
			echo "depositCoef[" . $value['id'] . "] = sum / 31104000;";

			//starting sum of deposis
			if ($value['current_sum'] != 0)
			{
//				$dateDiff = $value['datediff_seconds'];
//				echo "alert ('{$dateDiff}');";
				echo "$('#current_sum_" . $value['id'] . "').html(depositSums[" . $value['id'] . "] + Math.round((depositCoef[" . $value['id'] . "] * " . $value['datediff_seconds'] . ")*10000)/10000); ";
			}
		}
	?>

	setInterval('updateDeposits()', 1000);

	function updateDeposits()
	{
		for (var i = 0; i < depositCoef.length; ++i)
		{
			if (depositCoef[i] != undefined && $('#current_sum_' + i).html() != '0')
			{
				//alert(i + ': ' + depositCoef[i]);
				$('#current_sum_' + i).html(Math.round((parseFloat($('#current_sum_' + i).html()) + depositCoef[i])*10000)/10000);
			}
		}
	}
	
	function Withdraw(id)
	{
		if (depositSums[id] == 0)
		{
			alert('Deposit already withdrawed!');
		}
		else if (depositStates[id] == 0)
		{
			if (confirm(' <?php echo Language::Get('withdraw confirm'); ?> '))
			{
				window.location.assign('/deposits/withdraw/' + id);
			}
		}
		else
		{
			window.location.assign('/deposits/withdraw/' + id);
		}
	}
	
	function SumChange()
	{
		sum = parseInt($('#depositSum').val(), 10);
		if (isNum($('#depositSum').val()) == null || sum < 1)
		{
			alert('Wrong amount!');
			$('#depositSum').val('1');
		}
	}
	
	function BeforeSubmit()
	{
		if (parseInt($('#depositSum').val()) > money)
		{
			alert("Not enough money!");
			return false;
		}
		if (isNaN(parseInt($('#depositSum').val())) || parseInt($('#depositSum').val()) < 1)
		{
			alert('Wrong amount!');
			return false;
		}
		$('#depositForm').submit();
		return false;
	}

	function isNum(string)
	{
		//var numericExpression = /^[0-9]+$/;
		return string.match(/^[0-9]+$/); 

	}
</script>